/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;


import Model.Database;
import Model.Expertise;
import Model.Gender;
import Model.Menu;
import Model.OrderedRepas;
import Model.Repas;
import Model.Restaurant;
import Model.Restaurateur;
import Views.GestionRestaurant.AddRestaurantEvent;
import Views.GestionRestaurateur.AddRestaurateurEvent;
import Views.PasserCommande.CommandeFrame;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author ibrahim
 */
public class Controller {
    //DatabaseConnection dbc = new DatabaseConnection();
    Database db = new Database();
    String username=null;
    String adresse=null;
    int idRestaurant=0;
    String restaurantName=null;
    
   
    
    public void saisirInformationRestaurateur(AddRestaurateurEvent arev){
        String fname = arev.getFname();
        String lname = arev.getLname();
        int expertiseID = arev.getExpertise();
        boolean citizen = arev.isCitizen();
        String permanantCode = arev.getPermanantCode();
        String genderID = arev.getGender();
        Expertise expertise = null;
        Gender gender;
        
        switch(expertiseID){
            case 0:
                expertise = Expertise.junior;
                break;
            case 1:
                expertise = Expertise.intermediate;
                break;
            case 2:
                expertise = Expertise.senior;
                break;    
        }
        if (genderID.equals("male")){
            gender = Gender.male;
        } else {
            gender = Gender.female;
        }
        
        Restaurateur restaurateur = new Restaurateur(fname,lname,expertise,citizen,permanantCode,gender);
        db.insertRestaurateur(restaurateur);
    }
    public void saisirInformationRestaurant(AddRestaurantEvent arev){
        String name = arev.getName();
        int streetNum = arev.getStreetNum();
        String city = arev.getCity();
        String postalCode = arev.getPostalCode();
        String restaurateur = arev.getRestaurateurName();
        Restaurant restaurant = new Restaurant(name,streetNum,city,postalCode,restaurateur);
        db.insertRestaurant(restaurant);
        
    }
    
    public void assignerRestaurantARestaurateur(String restaurateurNom){
      
    }
    
    public void removeRestaurateur(int index){
        db.removeRestaurateur(index);
    }
    
    public void removeRestaurant(int index){
        db.removeRestaurant(index);
    }
    
    
    public int getRestaurateurId(int index){
        return db.getRestaurateurID(index);
    }
    
    public int getRestaurantId(int index){
        return db.getRestaurantID(index);
    }
    
    public List<Restaurateur>getRestauraterurs(){
        return db.getRestaurateurs();
    }
    
     public List<Restaurant>getRestaurants(){
        return db.getRestaurants();
    }
     
    public List<Repas> loadRepas(int id){
        return db.loadRepas(id);
    }
    
    public List<Repas> loadRepasFromMenu(int id) throws SQLException{
        return db.loadRepasFromMenu(id);
    }
    
    public void deleteRestaurateurFromDatabase(int id) throws SQLException{
        db.deleteFromDatabase(id);
    }
    
    public void deleteRestaurantFromDatabase(int id) throws SQLException{
        db.deleteRestaurantFromDatabase(id);
    }
    
    public void saveRestaurateurToDatabase() throws SQLException{
        db.save();
    }
    
    public void saveRestaurantToDatabase() throws SQLException{
        db.saveRestaurant();
    }
    
    public void checkLogin(String login, String pwd) throws SQLException{
        String[] client = db.checkLogin(login, pwd);
        username = client[0];
        adresse = client[1]+" "+client[2]+", "+client[3]+", "+client[4]+", "+client[5]+" "+client[6];
        if(username!=null){
            new CommandeFrame(this);
        }
    }
    
    public void addCommand(List<OrderedRepas> repas, float total, String date) throws SQLException{
        db.addCommand(repas, total, date, adresse, username);
    }
    
    public String getResultAction(){
        return db.getResultAction();
    }
    
    public void loadRestaurateursFromDatabase() throws SQLException{
        db.loadFromDatabase();
    }
    
    public void loadRestaurantsFromDatabase() throws SQLException{
        db.loadRestaurantFromDatabase();
    }
    
    public void startDatabaseConnection() throws Exception{
        db.connect();
    }
    
    
    public void closeDatabaseConnection() throws SQLException{
        db.disconnect();
    }
    
    public void saveRestaurateurToFile(File file) throws IOException{
        db.saveToFile(file);
    }
    
    public void saveRestaurantToFile(File file) throws IOException{
         db.saveRestaurantToFile(file);
    }
    
    public void loadRestaurateurFromFile(File file) throws IOException{
        db.loadFromFile(file);
    }
    
    public void loadRestaurantFromFile(File file) throws IOException{
         db.loadRestaurantFromFile(file);
    }
    
    public List<Menu> loadMenus(int idRestaurant) throws SQLException{
        return db.loadMenus(idRestaurant);
    }
    
    public void addMenu(String name) throws SQLException{
       db.addMenu(name, idRestaurant);
    }
    
    public void addRepas(String name, String description, float price, int id_menu) throws SQLException{
        db.addRepas(name, description, price, id_menu);
    }
    
    public void modifRepas(String name, String description, float price, int id_menu, int id_repas) throws SQLException{
        db.modifRepas(name, description, price, id_menu, id_repas);
    }
    
    public void setIdRestaurant(int id){
        idRestaurant=id;
    }
    
    public void setRestaurantName(String name){
        restaurantName=name;
    }
    
    public int getIdRestaurant(){
        return idRestaurant;
    }
    
    public String getRestaurantName(){
        return restaurantName;
    }
    
}
